package web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
//import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;


/**
 * 此过虑器解决GET与POST提交参数时的乱码
 * 实现原理其实就是对request进行了一次包装，重写他的getParameter方法
 */
//@WebFilter("/*")
public class EncodingFilter implements Filter {
    private final static String ENCODE="UTF-8";

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		if("GET".equals(req.getMethod().toUpperCase())){
			req=new EncodingRequest(req,ENCODE);
		}else{
			req.setCharacterEncoding(ENCODE);
		}
		response.setCharacterEncoding(ENCODE);
		chain.doFilter(req, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

	public void destroy() {
	}

}
